package com.android.rapidcash

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import com.android.lib.base.baseclass.BaseVMActivity
import com.android.lib.base.ext.initFragment
import com.android.lib.base.manager.ActivityManager
import com.android.lib.base.manager.LiveBusManager
import com.android.rapidcash.activitys.LoginActivity
import com.android.rapidcash.dialog.TipDialog
import com.android.rapidcash.fragments.HomeFragment
import com.android.rapidcash.fragments.MeFragment
import com.android.rapidcash.fragments.OrderFragment
import com.android.rapidcash.utils.CommUtils
import com.android.rapidcash.viewmodel.MainViewModel
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.rapidcash.abanca.santander.vivus.revolut.waylet.libertex.icmarkets.caixabank.R
import com.rapidcash.abanca.santander.vivus.revolut.waylet.libertex.icmarkets.caixabank.databinding.ActivityMainBinding
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.CommonPagerTitleView

class MainActivity : BaseVMActivity<MainViewModel, ActivityMainBinding>() {

    private val datas = arrayListOf("Inicio",  "Perfil")
    private val imgSe = arrayListOf(
        R.mipmap.icon_hone_home_s,
        R.mipmap.icon_hone_me_s
    )
    private val img = arrayListOf(
        R.mipmap.icon_hone_home,
        R.mipmap.icon_hone_me
    )
    private val fragments = arrayListOf<Fragment>(
        HomeFragment.newInstance(),
        MeFragment.newInstance(),
    )

    override fun initView(savedInstanceState: Bundle?) {
        mBinding.viewPager.initFragment(fragments)
        mBinding.viewPager.isUserInputEnabled = false
        mBinding.viewPager.offscreenPageLimit = 2
        bindMagicIndicator()
    }

    override fun initData() {

    }

    override fun initObserve() {
        viewModel.getVerifCode.observe(this) {
            LogUtils.e(GsonUtils.toJson(it))
        }
        LiveBusManager.observeInt(this, "Login") {
            when (it) {
                2 -> ToastUtils.showShort("进件页面")
                3 -> mBinding.viewPager.setCurrentItem(1, false)
                0 -> mBinding.viewPager.setCurrentItem(0, false)
            }
        }
    }


    private fun bindMagicIndicator(): CommonNavigator {
        val commonNavigator = CommonNavigator(this)
        commonNavigator.isAdjustMode = true
        commonNavigator.adapter = object : CommonNavigatorAdapter() {
            override fun getCount(): Int {
                return datas.size
            }

            override fun getTitleView(context: Context, index: Int): IPagerTitleView {
                val commonPagerTitleView = CommonPagerTitleView(context)


                // load custom layout
                val customLayout: View =
                    LayoutInflater.from(context).inflate(R.layout.item_bottom_main, null)
                val titleImg = customLayout.findViewById<ImageView>(R.id.img)
                val titleText = customLayout.findViewById<TextView>(R.id.title)
                titleImg.setImageResource(img[index])
                titleText.text = datas[index]
                commonPagerTitleView.setContentView(customLayout)

                commonPagerTitleView.onPagerTitleChangeListener = object :
                    CommonPagerTitleView.OnPagerTitleChangeListener {
                    override fun onSelected(index: Int, totalCount: Int) {
                        titleText.setTextColor(Color.parseColor("#FF6F6E"))
                        titleImg.setImageResource(imgSe[index])
                    }

                    override fun onDeselected(index: Int, totalCount: Int) {
                        titleText.setTextColor(Color.parseColor("#999999"))
                        titleImg.setImageResource(img[index])
                    }

                    override fun onLeave(
                        index: Int,
                        totalCount: Int,
                        leavePercent: Float,
                        leftToRight: Boolean
                    ) {
                    }

                    override fun onEnter(
                        index: Int,
                        totalCount: Int,
                        enterPercent: Float,
                        leftToRight: Boolean
                    ) {
                    }
                }

                commonPagerTitleView.setOnClickListener {
                    if (index == 1 && !CommUtils.isLogin()) {
                        startActivity(Intent(this@MainActivity, LoginActivity::class.java).apply {
                            putExtra("loginToPage", 3)
                        })
                        return@setOnClickListener
                    }
                    mBinding.viewPager.setCurrentItem(
                        index,
                        false
                    )
                }

                return commonPagerTitleView
            }

            override fun getIndicator(context: Context): IPagerIndicator? {
                return null
            }
        }
        mBinding.magicIndicator.navigator = commonNavigator
        mBinding.viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels)
                mBinding.magicIndicator.onPageScrolled(
                    position,
                    positionOffset,
                    positionOffsetPixels
                )
            }

            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                mBinding.magicIndicator.onPageSelected(position)

                when (position) {
                    0 -> (fragments[position] as HomeFragment).refresh()
                }

            }

            override fun onPageScrollStateChanged(state: Int) {
                super.onPageScrollStateChanged(state)
                mBinding.magicIndicator.onPageScrollStateChanged(state)
            }
        })
        return commonNavigator
    }

}